<?php

class Helper
{

    function getIp()
    {
        if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown")) {
            $ip = getenv("HTTP_X_FORWARDED_FOR");
        } elseif (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown")) {
            $ip = getenv("HTTP_CLIENT_IP");
        } elseif (getenv("HTTP_X_FORWARDED") && strcasecmp(getenv("HTTP_X_FORWARDED"), "unknown")) {
            $ip = getenv("HTTP_X_FORWARDED");
        } elseif (getenv("HTTP_FORWARDED_FOR") && strcasecmp(getenv("HTTP_FORWARDED_FOR"), "unknown")) {
            $ip = getenv("HTTP_FORWARDED_FOR");
        } elseif (getenv("HTTP_FORWARDED") && strcasecmp(getenv("HTTP_FORWARDED"), "unknown")) {
            $ip = getenv("HTTP_FORWARDED");
        } elseif (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown")) {
            $ip = getenv("REMOTE_ADDR");
        } else {
            $ip = 'unknown';
        }
        return $ip;
    }

    public static function getUserAgent()
    {
        return isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : null;
    }

    public static function getDateRand($length = 6, $type = 'num')
    {
        $code = date('YmdHis');
        if ($type == 'num') {
            $chars = '1234567890';
        } else if ($type == 'letter') {
            $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        } else if ($type == 'group') {
            $chars = '123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        } else if ($type == 'mix') {
            $chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()';
        }
        for ($i = 0; $i < $length; $i++) {
            $code .= $chars[mt_rand(0, strlen($chars) - 1)];
        }
        return $code;
    }

    public static function getRand($length = 6, $type = 'num')
    {
        $code = "";
        if ($type == 'num') {
            $chars = '1234567890';
        } else if ($type == 'letter') {
            $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        } else if ($type == 'group') {
            $chars = '123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        } else if ($type == 'mix') {
            $chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()';
        }
        for ($i = 0; $i < $length; $i++) {
            $code .= $chars[mt_rand(0, strlen($chars) - 1)];
        }
        return $code;
    }

    public static function writeover($fileName, $data, $method = 'rb+', $ifLock = true, $ifCheckPath = true, $ifChmod = true)
    {
        touch($fileName);
        $handle = fopen($fileName, $method);
        $ifLock && flock($handle, LOCK_EX);
        $writeCheck = fwrite($handle, $data);
        $method == 'rb+' && ftruncate($handle, strlen($data));
        fclose($handle);
        $ifChmod && @chmod($fileName, 0777);
        return $writeCheck;
    }

    public static function pw_var_export($input, $indent = '')
    {
        switch (gettype($input)) {
            case 'string' :
                return "'" . str_replace(array("\\", "'"), array("\\\\", "\'"), $input) . "'";
            case 'array' :
                $output = "array(\r\n";
                foreach ($input as $key => $value) {
                    $output .= $indent . "\t" . self::pw_var_export($key, $indent . "\t") . ' => ' . self::pw_var_export($value, $indent . "\t");
                    $output .= ",\r\n";
                }
                $output .= $indent . ')';
                return $output;
            case 'boolean' :
                return $input ? 'true' : 'false';
            case 'NULL' :
                return 'NULL';
            case 'integer' :
            case 'double' :
            case 'float' :
                return "'" . (string)$input . "'";
        }
        return 'NULL';
    }

    public static function sign($appkey, $appsecret, $batch)
    {
        $sign = md5($appkey . $appsecret . $batch);
        return $sign;
    }

    public static function getFmobile($country, $mobile)
    {
        $countrys = require(Yii::getAlias('@common/data/country.php'));
        if (isset($countrys[$country]) && !empty($countrys[$country])) {
            $key = $country;
        } else {
            $key = array_search($country, array_column($countrys, 'code', 'sname'));
        }
        $my = $countrys[$key]['code'];
        if ($key != "CN") {
            $mobile = "+" . $my . $mobile;
        }
        return $mobile;
    }

    public static function isMobile($mobile, $abroad = false)
    {
        if ($abroad === true) {
            $mobile = str_replace("+", "", $mobile);
            $phone_pattern = "/^\d{5,15}$/";
        } else {
            $phone_pattern = "/^(13[0-9]|15[0-9]|18[0-9]|14[5678]|16[6]|19[89]|17[0-9])[0-9]{8}$/";
        }
        if (preg_match($phone_pattern, $mobile)) {
            return true;
        } else {
            return false;
        }
    }

    public static function isEmail($email)
    {
        $email_pattern = "/^([\.a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/";
        if (preg_match($email_pattern, $email)) {
            return true;
        }
        return false;
    }

    public static function encrypt($data, $key)
    {
        $key = md5($key);
        $x = 0;
        $len = strlen($data);
        $l = strlen($key);
        $char = '';
        $str = '';
        for ($i = 0; $i < $len; $i++) {
            if ($x == $l) {
                $x = 0;
            }
            $char .= $key{$x};
            $x++;
        }
        for ($i = 0; $i < $len; $i++) {
            $str .= chr(ord($data{$i}) + (ord($char{$i})) % 256);
        }
        return base64_encode($str);
    }

    public static function decrypt($data, $key)
    {
        $char = '';
        $str = '';
        $key = md5($key);
        $x = 0;
        $data = base64_decode($data);
        $len = strlen($data);
        $l = strlen($key);
        for ($i = 0; $i < $len; $i++) {
            if ($x == $l) {
                $x = 0;
            }
            $char .= substr($key, $x, 1);
            $x++;
        }
        for ($i = 0; $i < $len; $i++) {
            if (ord(substr($data, $i, 1)) < ord(substr($char, $i, 1))) {
                $str .= chr((ord(substr($data, $i, 1)) + 256) - ord(substr($char, $i, 1)));
            } else {
                $str .= chr(ord(substr($data, $i, 1)) - ord(substr($char, $i, 1)));
            }
        }
        return $str;
    }


}


?>